package cn.zjyj.module.zjyj.controller.admin.riskassessmentinstancerel;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.zjyj.framework.common.common.pojo.PageParam;
import cn.zjyj.framework.common.common.pojo.PageResult;
import cn.zjyj.framework.common.common.pojo.CommonResult;
import cn.zjyj.framework.common.common.util.object.BeanUtils;
import static cn.zjyj.framework.common.common.pojo.CommonResult.success;

import cn.zjyj.framework.excel.core.util.ExcelUtils;

import cn.zjyj.framework.apilog.core.annotation.ApiAccessLog;
import static cn.zjyj.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.zjyj.module.zjyj.controller.admin.riskassessmentinstancerel.vo.*;
import cn.zjyj.module.zjyj.dal.dataobject.riskassessmentinstancerel.RiskAssessmentInstanceRelDO;
import cn.zjyj.module.zjyj.service.riskassessmentinstancerel.RiskAssessmentInstanceRelService;

@Tag(name = "管理后台 - 风险评估实例关系")
@RestController
@RequestMapping("/zjyj/risk-assessment-instance-rel")
@Validated
public class RiskAssessmentInstanceRelController {

    @Resource
    private RiskAssessmentInstanceRelService riskAssessmentInstanceRelService;

    @PostMapping("/create")
    @Operation(summary = "创建风险评估实例关系")
    @PreAuthorize("@ss.hasPermission('zjyj:risk-assessment-instance-rel:create')")
    public CommonResult<Long> createRiskAssessmentInstanceRel(@Valid @RequestBody RiskAssessmentInstanceRelSaveReqVO createReqVO) {
        return success(riskAssessmentInstanceRelService.createRiskAssessmentInstanceRel(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新风险评估实例关系")
    @PreAuthorize("@ss.hasPermission('zjyj:risk-assessment-instance-rel:update')")
    public CommonResult<Boolean> updateRiskAssessmentInstanceRel(@Valid @RequestBody RiskAssessmentInstanceRelSaveReqVO updateReqVO) {
        riskAssessmentInstanceRelService.updateRiskAssessmentInstanceRel(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除风险评估实例关系")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('zjyj:risk-assessment-instance-rel:delete')")
    public CommonResult<Boolean> deleteRiskAssessmentInstanceRel(@RequestParam("id") Long id) {
        riskAssessmentInstanceRelService.deleteRiskAssessmentInstanceRel(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除风险评估实例关系")
                @PreAuthorize("@ss.hasPermission('zjyj:risk-assessment-instance-rel:delete')")
    public CommonResult<Boolean> deleteRiskAssessmentInstanceRelList(@RequestParam("ids") List<Long> ids) {
        riskAssessmentInstanceRelService.deleteRiskAssessmentInstanceRelListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得风险评估实例关系")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('zjyj:risk-assessment-instance-rel:query')")
    public CommonResult<RiskAssessmentInstanceRelRespVO> getRiskAssessmentInstanceRel(@RequestParam("id") Long id) {
        RiskAssessmentInstanceRelDO riskAssessmentInstanceRel = riskAssessmentInstanceRelService.getRiskAssessmentInstanceRel(id);
        return success(BeanUtils.toBean(riskAssessmentInstanceRel, RiskAssessmentInstanceRelRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得风险评估实例关系分页")
    @PreAuthorize("@ss.hasPermission('zjyj:risk-assessment-instance-rel:query')")
    public CommonResult<PageResult<RiskAssessmentInstanceRelRespVO>> getRiskAssessmentInstanceRelPage(@Valid RiskAssessmentInstanceRelPageReqVO pageReqVO) {
        PageResult<RiskAssessmentInstanceRelDO> pageResult = riskAssessmentInstanceRelService.getRiskAssessmentInstanceRelPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, RiskAssessmentInstanceRelRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出风险评估实例关系 Excel")
    @PreAuthorize("@ss.hasPermission('zjyj:risk-assessment-instance-rel:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportRiskAssessmentInstanceRelExcel(@Valid RiskAssessmentInstanceRelPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<RiskAssessmentInstanceRelDO> list = riskAssessmentInstanceRelService.getRiskAssessmentInstanceRelPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "风险评估实例关系.xls", "数据", RiskAssessmentInstanceRelRespVO.class,
                        BeanUtils.toBean(list, RiskAssessmentInstanceRelRespVO.class));
    }

}